package com.beiding.dao.impl;

import com.beiding.dao.ResourceOperations;
import com.beiding.pojo.Room;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class RoomDaoImpl implements ResourceOperations<Room> {

    private MongoTemplate mongoTemplate;

    public RoomDaoImpl(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
    }

    public List<Room> nearbyRooms(double elng, double wlng, double nlat, double slat, Pageable pageable){

        Query query = Query.query(
                Criteria
                        .where("published").is(true)    //已经发布
                        .and("theUser").is(null)         //没有被使用
                        .and("freeze").is(false)        //未被冻结
                        .and("location.longitude").lt(elng).gt(wlng)
                        .and("location.latitude").lt(nlat).gt(slat)
        ).with(pageable);

        //只包含id和name,地址,价格,图片
        query.fields().include("id").include("name").include("location").include("prices").include("images");
        return mongoTemplate.find(query, Room.class);
    }


    @Override
    public Class<Room> type() {
        return Room.class;
    }

    @Override
    public MongoOperations mongoOperations() {
        return mongoTemplate;
    }

}